//
// Created by entaoyang@163.com on 2021/7/23.
//

import Foundation
import SwiftSweet

class AA:Codable {
    var nameNick: String = "yang"
}

class Person: AA {
    var age: Int = 40
    
    func dump(_ prefix: String = "Begin: ") {
        println(prefix, nameNick, age)
    }
    
    enum Keys:String, CodingKey {
        case nameNick
        case age
    }
}

func testCode() {
    testEncode()
}

func testEncode() {
    let p = Person()
    let d = try? p.toYson ()
    println(d?.yson)
    println("END")
}


func testArray() {
    let ya = ysonArray([1, 2, nil, 4])
    let d = JsonDecoder(value: ya)
    do {
        let y: [Int?] = try [Int?](from: d)
        for n in y {
            print(n ?? "nil", terminator: ",")
        }
        print()
    } catch (let e) {
        logd(e)
    }
    print("END")
}

func testObject() {

    let yo = yson {
        "name" >> "yang"
        "age" >> 99
        "bbb" >> 123
        "birth" >> "1981-02-18"
        "skills" >> ["A", nil, "B"]
        "son" >> yson {
            "name" >> "dou"
            "age" >> 10
            "birth" >> "2011-03-21"
            "skills" >> ["C", "D"]
        }
    }
    let yang: Person? = yo.toModel { opt in
        opt.nameMap(field: "nameNick", json: "name")
        opt.push(DateFormatJsonDecoder.date)
    }
    yang?.dump()


//    do {
//        let y: Yang = try Yang(from: d)
//        y.dump()
//    } catch (let e) {
//        logd(e)
//    }
    print("END")
}

func testKeyAny() {
    let f: Double = 99.9
    let b = "user" >> {
        "name" >> "Entao"
        "age" >> f
        "some" >> [1, 2, 3]
        "children" >> {
            "person" >> {
                "name" >> "SUO"
                "age" >> 9
            }
        }
        "address" >> nil
    }
    logd(b)
    let c = "user" >> {
        "name" >> "SUO"
        "age" >> 99
        "children2" >> ["a", 9, nil,
                        "d" >> {
                            "addr" >> "JiNan"
                        }
        ]
        "hello" >> ["a": 1, "b": 2]

    }
    logd(c)
}
